from random import choice
from earthdawn.utils import flatten
import time

def removeLinkDeadActors(Select, Update, aux_queue):
    allPlayersInRooms = flatten(Select.getAllPlayers())
    allCurrentPlayers = flatten(Select.getAllPlayersWithIP())
    
    linkDeadPlayers = filter(lambda x:x not in allCurrentPlayers,allPlayersInRooms)+ filter(lambda x:x not in allPlayersInRooms,allCurrentPlayers)
    for each in linkDeadPlayers:
        Update.setLocation(each, 0)
    # set up to refire
    aux_queue.append((time.time() + 300 , 'removeLinkDeadActors(Select, Update, aux_queue)'))

def saveAllPlayers(Select, Update):
    allCurrentPlayers = flatten(Select.getAllPlayersWithIP())

    # we'll have to set the following of all active players:
    # email, description, colors, discipline, circle, age, spouse_name, dexterity, strength, toughness, perception, 
    # willpower, charisma, initiative, fly, max_carry, max_lift, physical_defense, spell_defense, social_defense, 
    # normal_armor, mystical_armor, wound_threshold, damage, wound, unconscious_rating, death_rating, blood_magic_cost, 
    # recovery_tests_left, recovery_tests_total, karma_dice, karma_current, karma_max, legend_total, passion
    for each in allCurrentPlayers:
        data = (sessions[each].description, sessions[each].discipline, sessions[each].circle, \
                sessions[each].age, sessions[each].spouse_name, sessions[each].dexterity, sessions[each].strength, sessions[each].toughness, \
                sessions[each].perception, sessions[each].willpower, sessions[each].charisma, sessions[each].initiative, sessions[each].fly, \
                sessions[each].max_carry, sessions[each].max_lift, sessions[each].physical_defense, sessions[each].spell_defense, \
                sessions[each].social_defense, sessions[each].normal_armor, sessions[each].mystical_armor, sessions[each].wound_threshold, \
                sessions[each].damage, sessions[each].wound, sessions[each].unconscious_rating, sessions[each].death_rating, \
                sessions[each].blood_magic_cost, sessions[each].recovery_tests_left, sessions[each].recovery_tests_total, \
                sessions[each].karma_dice, sessions[each].karma_current, sessions[each].karma_max, sessions[each].legend_total, \
                sessions[each].passion)

        Update.savePlayer(data, each)
        
def saveRandomPlayer(sessions, Select, Update, aux_queue):
    allCurrentPlayers = list(flatten(Select.getAllPlayersWithIP()))
    if len(allCurrentPlayers) > 0:
        each = choice(allCurrentPlayers)
    
        data = (sessions[each].description, sessions[each].discipline, sessions[each].circle, \
                sessions[each].age, sessions[each].spouse_name, sessions[each].dexterity, sessions[each].strength, sessions[each].toughness, \
                sessions[each].perception, sessions[each].willpower, sessions[each].charisma, sessions[each].initiative, sessions[each].fly, \
                sessions[each].max_carry, sessions[each].max_lift, sessions[each].physical_defense, sessions[each].spell_defense, \
                sessions[each].social_defense, sessions[each].normal_armor, sessions[each].mystical_armor, sessions[each].wound_threshold, \
                sessions[each].damage, sessions[each].wound, sessions[each].unconscious_rating, sessions[each].death_rating, \
                sessions[each].blood_magic_cost, sessions[each].recovery_tests_left, sessions[each].recovery_tests_total, \
                sessions[each].karma_dice, sessions[each].karma_current, sessions[each].karma_max, sessions[each].legend_total, \
                sessions[each].passion)
    
        Update.savePlayer(data, each)
    # set up to refire
    aux_queue.append((time.time() + 300 , 'saveRandomPlayer(Select, Update, aux_queue)'))
        
def savePlayer(each, Update):
    try:    
        data = (sessions[each].description, sessions[each].discipline, sessions[each].circle, \
                sessions[each].age, sessions[each].spouse_name, sessions[each].dexterity, sessions[each].strength, sessions[each].toughness, \
                sessions[each].perception, sessions[each].willpower, sessions[each].charisma, sessions[each].initiative, sessions[each].fly, \
                sessions[each].max_carry, sessions[each].max_lift, sessions[each].physical_defense, sessions[each].spell_defense, \
                sessions[each].social_defense, sessions[each].normal_armor, sessions[each].mystical_armor, sessions[each].wound_threshold, \
                sessions[each].damage, sessions[each].wound, sessions[each].unconscious_rating, sessions[each].death_rating, \
                sessions[each].blood_magic_cost, sessions[each].recovery_tests_left, sessions[each].recovery_tests_total, \
                sessions[each].karma_dice, sessions[each].karma_current, sessions[each].karma_max, sessions[each].legend_total, \
                sessions[each].passion)
    
        Update.savePlayer(data, each)
    except:
        print "SAVE PLAYER %i FAILED"